home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / applet / overview / example / PrintThread.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  2.3 KB  |  75 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.applet.Applet;
  18. import java.awt.Graphics;
  19. import java.awt.TextArea;
  20.  
  21. public class PrintThread extends Applet {
  22.  
  23.     java.awt.TextArea display = new java.awt.TextArea(1, 80);
  24.     int paintCount = 0;
  25.  
  26.     public void init() {
  27.         //Create the text area and make it uneditable.
  28.         display = new TextArea(1, 80);
  29.         display.setEditable(false);
  30.  
  31.     //Set the layout manager so that the text area 
  32.     //will be as wide as possible.
  33.         setLayout(new java.awt.GridLayout(1,0));
  34.  
  35.     //Add the text area to the applet.
  36.         add(display);
  37.         validate();
  38.  
  39.         addItem("init: " + threadInfo(Thread.currentThread()));
  40.     }
  41.  
  42.     public void start() {
  43.         addItem("start: " + threadInfo(Thread.currentThread()));
  44.     }
  45.  
  46.     public void stop() {
  47.         addItem("stop: " + threadInfo(Thread.currentThread()));
  48.     }
  49.  
  50.     public void destroy() {
  51.         addItem("destroy: " + threadInfo(Thread.currentThread()));
  52.     }
  53.  
  54.     String threadInfo(Thread t) {
  55.         return "thread=" + t.getName() + ", "
  56.                + "thread group=" + t.getThreadGroup().getName();
  57.     }
  58.    
  59.     void addItem(String newWord) {
  60.         System.out.println(newWord);
  61.         display.appendText(newWord + "\n");
  62.         display.repaint();
  63.         //A hack to get the applet update() method called
  64.         //occasionally:
  65.         if (++paintCount % 4 == 0) {
  66.             repaint();
  67.         }
  68.     }
  69.  
  70.     public void update(Graphics g) {
  71.         addItem("update: " + threadInfo(Thread.currentThread()));
  72.         super.update(g);
  73.     }
  74. }
  75.